
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */
import javax.swing.*;
import java.text.*;
import java.sql.*;

final class ViewProfile extends javax.swing.JInternalFrame {

    Object[][] tableObject=new Object[][]{};
    Systems systems=new Systems();
        PreparedStatement statement;
        String codePrefix;
        Thread processThread=new Thread();
        Thread titleThread=new Thread();
    /** Creates new form ViewPendingCreditOrder */
    public ViewProfile() {
        initComponents();
        reset();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jCodePrefixTextField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jCancelButton = new javax.swing.JButton();
        jRefreshButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jRefTableTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(ViewProfile.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jCodePrefixTextField.setEditable(false);
        jCodePrefixTextField.setName("jCodePrefixTextField"); // NOI18N
        jCodePrefixTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCodePrefixTextFieldActionPerformed(evt);
            }
        });

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jCancelButton.setIcon(resourceMap.getIcon("jCancelButton.icon")); // NOI18N
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setName("jCancelButton"); // NOI18N
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        jRefreshButton.setIcon(resourceMap.getIcon("jRefreshButton.icon")); // NOI18N
        jRefreshButton.setText(resourceMap.getString("jRefreshButton.text")); // NOI18N
        jRefreshButton.setName("jRefreshButton"); // NOI18N
        jRefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefreshButtonActionPerformed(evt);
            }
        });

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jRefTableTextField.setEditable(false);
        jRefTableTextField.setName("jRefTableTextField"); // NOI18N
        jRefTableTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefTableTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRefTableTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCodePrefixTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75)
                .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jCodePrefixTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jRefTableTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "S/N", "Attribute", "Type", "String Length", "Referenced Table", "Default", "Optional"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title5")); // NOI18N
        jTable1.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N
        jTable1.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("jTable1.columnModel.title6")); // NOI18N
        jTable1.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("jTable1.columnModel.title5")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1094, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void printPreviewButtonClicked(){
        
    }

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

        String multiRow=systems.getValue(OpenMSApp.Database_A, "Select * from vwProfile where vName='"+jComboBox1.getSelectedItem().toString()+"'", "cMultiRow");
        if(multiRow.trim().equalsIgnoreCase("0"))        
        {
            jRefTableTextField.setText("N/A");
        }
        else if(multiRow.trim().equalsIgnoreCase("1"))        
        {
            String refTableCode=systems.getValue(OpenMSApp.Database_A, "Select * from vwProfile where vName='"+jComboBox1.getSelectedItem().toString()+"'", "cPrimaryTableCode");
            String primaryTable=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where cReferenceTableCode='"+refTableCode+"'", "vName");
            jRefTableTextField.setText(primaryTable);
        }
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
                disableControls();
                profileCodeSelected();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewProfileThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
    }//GEN-LAST:event_jComboBox1ActionPerformed
    private void profileCodeSelected(){
        
        String orderCode=jComboBox1.getSelectedItem().toString();
        resetField();

        if(orderCode.equals("-- Select --"))
        {
            reset();
        }
       else
       {
            codePrefix=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+orderCode+"'", "cPrefix");           
            jCodePrefixTextField.setText(codePrefix.trim());
            refreshTable();
            setTitle("::. View Profile");
        }
    }
    void print()
    {
        try
        {
            jTable1.print();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private void jCodePrefixTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCodePrefixTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCodePrefixTextFieldActionPerformed

private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed

        if(processThread.isAlive())
        {
            try{
                processThread.stop();
                titleThread.stop();
                System.out.println("Threads successfully stopped.");
            }catch(Exception e){System.out.println("Thread stopping error: "+e);}
            this.setTitle("::. View Profile");
            enableControls();
        }
        else
        dispose();
}//GEN-LAST:event_jCancelButtonActionPerformed

private void jRefreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshButtonActionPerformed

    if(jComboBox1.getSelectedItem().toString().equalsIgnoreCase("-- Select --"))
        reset();
    else{
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("titleThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
    }
}//GEN-LAST:event_jRefreshButtonActionPerformed

private void jRefTableTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefTableTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jRefTableTextFieldActionPerformed

    
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. View Profile - Processing Request");
            thread.sleep(500);
            this.setTitle("::. View Profile - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. View Profile - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. View Profile - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
    private void disableControls()
    {
        jComboBox1.setEnabled(false);
        jRefreshButton.setEnabled(false);
    }
    private void enableControls()
    {
        jComboBox1.setEnabled(true);
        jRefreshButton.setEnabled(true);
        if(jComboBox1.getSelectedItem().toString().equalsIgnoreCase("-- Select --"))
        setTitle("::. View Profile");
        else
        setTitle("::. View Profile");
    }
    public void resetProfileCode()
    {
        String[] orderCode=systems.getTableDataArray(OpenMSApp.Database_A,"vwProfile",2);
        orderCode[0]="-- Select --";
        jComboBox1.setModel(new DefaultComboBoxModel(orderCode));
        setTitle("::. View Profile");
    }
    public void resetTable()
    {
        tableObject=new Object[][]{};
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "S/N", "Attribute", "Type", "String Length", "Referenced Table", "Default", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
    }
    public void resetField()
    {
        jCodePrefixTextField.setText("");
    }
    public void resetCombo()
    {
        resetProfileCode();
    }
    public void reset()
    {
        resetField();
        resetTable();
        resetCombo();
    }
    public void refreshTable()
    {
        String refTableCode=systems.getValue(OpenMSApp.Database_A, "Select * from referencetable where vName='"+jComboBox1.getSelectedItem().toString()+"'", 1);
        String[][] profileTable=systems.getColumn(OpenMSApp.Database_A,"Select * from Profile where cProfileCode='"+refTableCode+"' order by cProfileCode,iSerial",1,2,3,4,5,6,7,8);
        String profileCode[]=profileTable[0];
        String[] attribute=profileTable[1];
        String[] type=profileTable[2];
        String[] defaultValue=profileTable[3];
        String[] stringLength=profileTable[4];
        String[] referencedTable=profileTable[5];  
        String[] optional=profileTable[6];        
        String[] editable=profileTable[7];      
        
        int x=0;
        int y=profileCode.length;

        tableObject=new Object[y][8];

        while(x<y)
        {            
            tableObject[x][0]=x+1;
            tableObject[x][1]=attribute[x].trim();
            tableObject[x][2]=type[x].trim();
            if(type[x].trim().equalsIgnoreCase("Decimal")&&defaultValue[x]!=null)
                try
                {
                    tableObject[x][3]=systems.converToRealMoneyFormat(defaultValue[x].trim());
                }
                catch(Exception e)
                {
                    tableObject[x][3]="Er!M1C01K0";
                }
            else if(defaultValue[x]==null)            
                tableObject[x][3]="";
            else            
                tableObject[x][3]=defaultValue[x].trim();
            if(stringLength[x]==null||stringLength[x].equalsIgnoreCase("0"))stringLength[x]="";
            tableObject[x][4]=stringLength[x].trim();
            tableObject[x][5]=referencedTable[x].trim();
            if(optional[x].trim().equalsIgnoreCase("1"))
                tableObject[x][6]=true;
            else
                tableObject[x][6]=false;
            if(editable[x].trim().equalsIgnoreCase("1"))
                tableObject[x][7]=true;
            else
                tableObject[x][7]=false;
                
            systems.freeHeap();
        x+=1;
        if(x==y){break;}
       }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "S/N", "Attribute", "Type", "Default", "String Length", "Referenced Table", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancelButton;
    private javax.swing.JTextField jCodePrefixTextField;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jRefTableTextField;
    private javax.swing.JButton jRefreshButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
